package com.xiaomili.halfsugar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.bean.ProductBean;


/**
 * 搜索页面---GridViewItem适配器
 * Created by Administrator on 2016/7/20.
 */
public class SearchProductGridView_Adapter extends BaseAdapter {
    private Context mContext;
    private ProductBean mList;
    private int mIndex;

    public SearchProductGridView_Adapter(Context context, ProductBean mList, int index) {
        this.mContext = context;
        this.mList = mList;
        this.mIndex = index;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.data.get(mIndex).subclass.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridviewHolder gridviewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.product_radiobutton_gridview, parent, false);
            gridviewHolder = new GridviewHolder(convertView);
            convertView.setTag(gridviewHolder);
        } else {
            gridviewHolder = (GridviewHolder) convertView.getTag();
        }
        Picasso.with(mContext).load(mList.data.get(mIndex).subclass.get(position).icon)
                .placeholder(R.mipmap.ic_launcher)  //占位图片 ，
                .error(R.mipmap.ic_launcher)
                .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                .into(gridviewHolder.imageView);
        gridviewHolder.textView.setText(mList.data.get(mIndex).subclass.get(position).name);
        return convertView;
    }

    private class GridviewHolder {
        private ImageView imageView;
        private TextView textView;

        public GridviewHolder(View convertView) {
            imageView = (ImageView) convertView.findViewById(R.id.search_product_gridview);
            textView = (TextView) convertView.findViewById(R.id.search_product_tv);
        }
    }
}
